package com.ihome.android;


import java.util.Collection;
import java.util.Stack;

import android.app.AlertDialog;

import com.ihome.android.util.log.LogUtil;

public class EADialogMgr {
	private static final String			TAG		= EADialogMgr.class.getSimpleName();
	public static String				logDir	= "";
	private static Stack<AlertDialog>	stack;
	private static EADialogMgr		instance;

	private EADialogMgr() {}

	public synchronized AlertDialog add(AlertDialog dialog) {
		if (stack == null) {
			stack = new Stack<AlertDialog>();
		}
		stack.add(dialog);
		LogUtil.d(TAG, "activityStack size---->" + stack.size());
		return dialog;
	}

	/**
	 * 单一实例
	 */
	public static EADialogMgr getDialogManager() {
		if (instance == null) {
			instance = new EADialogMgr();
		}
		if (stack == null) {
			stack = new Stack<AlertDialog>();
		}
		return instance;
	}

	public boolean empty() {
		return stack.empty();
	}

	public synchronized AlertDialog peek() {
		return stack.peek();
	}

	public synchronized AlertDialog pop() {
		return stack.pop();
	}

	public synchronized AlertDialog push(AlertDialog dialog) {
		if (stack == null) {
			stack = new Stack<AlertDialog>();
		}
		if (contains(dialog)) {
			LogUtil.d(TAG, "push contains(dialog)" + dialog.getClass().getName());
			remove(dialog);
		}
		AlertDialog pushDialog = stack.push(dialog);
		LogUtil.d(TAG, "push size--->" + stack.size());
		return pushDialog;
	}

	public synchronized AlertDialog remove(int i) {
		return stack.remove(i);
	}

	public synchronized boolean remove(AlertDialog dialog) {
		return stack.remove(dialog);
	}

	public synchronized boolean removeAll(Collection<AlertDialog> collection) {
		return stack.removeAll(collection);
	}

	public synchronized int search(AlertDialog dialog) {
		return stack.search(dialog);
	}

	public synchronized int size() {
		return stack.size();
	}

	public synchronized boolean contains(AlertDialog dialog) {
		return stack.contains(dialog);
	}

	public synchronized AlertDialog get(int location) {
		return stack.get(location);
	}

	/**
	 * 判断当前Activity是否在最上面
	 */
	public boolean isTopDialog(AlertDialog dialog) {
		boolean isTop = false;
		if (dialog != null) {
			if (dialog == stack.peek()) {
				isTop = true;
			}
			else {
				isTop = false;
			}
		}
		LogUtil.v(EADialogMgr.this, " isTopActivity -- " + dialog.toString() + " -- is" + isTop);
		return isTop;
	}

	/**
	 * 结束指定的Dialog
	 */
	public void dismiss(AlertDialog dialog) {
		if (dialog != null) {
			stack.remove(dialog);
			dialog.dismiss();
			dialog = null;
		}
	}

	/**
	 * 移除指定的Dialog
	 */
	public void removeDialog(AlertDialog dialog) {
		if (dialog != null) {
			stack.remove(dialog);
			dialog = null;
		}
	}

	/**
	 * 结束指定类名的Dialog
	 */
	public void dismiss(Class<?> cls) {
		for (AlertDialog dialog : stack) {
			if (dialog.getClass().equals(cls)) {
				dismiss(dialog);
			}
		}
	}

	/**
	 * 结束所有Dialog
	 */
	public void dismissAll() {
		for (int i = size() - 1; i >= 0; i--) {
			LogUtil.d(TAG, "  结束之前 size-->" + size());
			if (null != stack.get(i)) {
				LogUtil.d(TAG, " dismissAll Dialog" + i + ":-->" + stack.get(i).getClass().getName());
				dismiss(stack.get(i));
			}
			else {
				LogUtil.d(TAG, " dismissAll  null " + i + ":-->" + stack.get(i).getClass().getName());
			}
		}
		LogUtil.d(TAG, "  结束之后 size-->" + size());
		stack.clear();
	}


}